package com.chainyoung.component_tmperson.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chainyoung.common.utils.TimeUtils;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.entity.BrowseCache;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;


public class BrowserHistoryAdapter extends RecyclerView.Adapter<BrowserHistoryAdapter.GroupViewHolder> {

    static final int VIEW_TYPE_NON_STICKY = R.layout.layout_browser_history_item;
    static final int VIEW_TYPE_STICKY = R.layout.layout_browse_history_sticky;

    private List<BrowseCache> mListItems = new ArrayList<>();

    private View.OnClickListener onClickListener;
    private View.OnLongClickListener onLongClickListener;

    @Override
    public GroupViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(viewType, parent, false);
        return new GroupViewHolder(view);
    }

    @Override
    public void onBindViewHolder(GroupViewHolder holder, int position) {
        holder.bind(position);
    }

    @Override
    public int getItemViewType(int position) {
        if (mListItems.get(position).getUrl() == null) {
            return VIEW_TYPE_STICKY;
        }
        return VIEW_TYPE_NON_STICKY;
    }

    @Override
    public int getItemCount() {
        return mListItems.size();
    }

    public void setListItems(List<BrowseCache> newItems) {
        mListItems.clear();
        for (BrowseCache item : newItems) {
            mListItems.add(item);
        }

        Collections.sort(mListItems, new Comparator<BrowseCache>() {
            @Override
            public int compare(BrowseCache o1, BrowseCache o2) {
                return o2.getUpdtime().compareToIgnoreCase(o1.getUpdtime());
            }
        });

        BrowseCache stickyListItem = null;
        for (int i = 0, size = mListItems.size(); i < size; i++) {
            BrowseCache listItem = mListItems.get(i);
            String time = listItem.getTime();
            if (stickyListItem == null || !stickyListItem.getTime().equals(time)) {
                stickyListItem = new BrowseCache(time);
                mListItems.add(i, stickyListItem);
                size += 1;
            }
        }
        notifyDataSetChanged();
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setOnLongClickListener(View.OnLongClickListener onLongClickListener) {
        this.onLongClickListener = onLongClickListener;
    }

    public List<BrowseCache> getmListItems() {
        return mListItems;
    }

    class GroupViewHolder extends RecyclerView.ViewHolder {

        private TextView tvTitle;
        private TextView tvUrl;
        private RelativeLayout relativeLayout;

        GroupViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvUrl = itemView.findViewById(R.id.tv_url);
            relativeLayout=itemView.findViewById(R.id.rl_url);
        }

        void bind(int p) {
            BrowseCache item = mListItems.get(p);

            if (BrowserHistoryAdapter.this.getItemViewType(p) == VIEW_TYPE_STICKY) {
                int isYesterDay = 1;
                try {
                    isYesterDay = TimeUtils.isYeaterday(item.getTime(), new Date());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                if (isYesterDay == -1) {
                    tvTitle.setText("今天");
                } else if (isYesterDay == 0) {
                    tvTitle.setText("昨天");
                } else {
                    tvTitle.setText(item.getTime());
                }
            } else {
                tvTitle.setText(item.getTitle());
                tvUrl.setText(item.getUrl());
                relativeLayout.setOnClickListener(onClickListener);
                relativeLayout.setOnLongClickListener(onLongClickListener);
                relativeLayout.setTag(p);
            }
        }
    }


}



